<template>
  <div class="admin-login-wrapper">
    <div class="site-title">数学建模校赛网站</div>
    <div class="subtitle">——管理员登录页面</div>
    <div class="login-box">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="70px">
        <el-form-item label="用户名" prop="username" required>
          <el-input v-model="form.username" placeholder="请输入管理员用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password" required>
          <el-input v-model="form.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="角色">
          <el-radio checked disabled />
          <span class="role-label">admin</span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitLogin" class="login-btn" style="width:100%">登录</el-button>
        </el-form-item>
        <div v-if="error" class="error">{{ error }}</div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const formRef = ref(null)
const error = ref('')
const form = ref({
  username: '',
  password: ''
})
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const submitLogin = () => {
  error.value = ''
  formRef.value.validate((valid) => {
    if (!valid) return
    if (form.value.username === 'admin' && form.value.password === '123456') {
      localStorage.setItem('userRole', 'admin')
      localStorage.setItem('username', 'admin')
      router.push({ name: 'CreateContest' })
    } else {
      error.value = '用户名或密码错误'
    }
  })
}
</script>

<style scoped>
.admin-login-wrapper {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 3px solid #bfc8da;
  box-sizing: border-box;
  padding: 40px 0;
  background: #f8faff;
}
.site-title {
  font-size: 2.4rem;
  font-weight: bold;
  margin-bottom: 0.5em;
  letter-spacing: 2px;
}
.subtitle {
  font-size: 1.1rem;
  color: #666;
  margin-bottom: 2em;
  text-align: right;
  width: 400px;
}
.login-box {
  border: 2px solid #6b7ba3;
  border-radius: 8px;
  padding: 2em 2.5em 1.5em 2.5em;
  background: #fff;
  width: 400px;
  box-sizing: border-box;
}
.form-item {
  display: flex;
  align-items: center;
  margin-bottom: 1.2em;
}
.form-item label {
  width: 70px;
  text-align: right;
  margin-right: 10px;
  font-size: 1rem;
}
.form-item input[type="text"],
.form-item input[type="password"] {
  flex: 1;
  padding: 0.5em;
  font-size: 1rem;
  border: 1px solid #bfc8da;
  border-radius: 4px;
}
.form-item.role {
  align-items: center;
}
.form-item input[type="radio"] {
  accent-color: #3a5ba0;
  margin-right: 6px;
}
.role-label {
  font-weight: bold;
  color: #3a5ba0;
  margin-right: 8px;
}
.role-tip {
  color: #888;
  font-size: 0.95em;
}
.login-btn {
  width: 100%;
  padding: 0.6em 0;
  background: #bfc8da;
  color: #222;
  border: none;
  border-radius: 4px;
  font-size: 1.1rem;
  cursor: pointer;
  transition: background 0.2s;
}
.login-btn:hover {
  background: #a0b3d6;
}
.error {
  color: #d32f2f;
  margin-top: 0.8em;
  text-align: center;
}
</style>

